package com.example.demo.api;

import com.example.demo.entity.User;
import com.example.demo.service.imp.UserImp;
import com.example.demo.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @Date:2021/6/29 11:16
 * @Author:郑伟
 * @Version:1.1.0
 */
@RestController
@RequestMapping("api/user")
public class UserRestController {
    @Autowired
    private UserImp userImp;
    @PostMapping("/login")
    public Result<User> login(@RequestParam("name") String name, @RequestParam("password") String password, HttpServletRequest httpServletRequest){
        int code=userImp.login(name,password);
        Result<User> result = new Result<>();
        if (code>0){
            httpServletRequest.getSession().setAttribute("name",name);
            result.ok(new User(null,name,password,"email","sex"),200,"登录成功!");
        }else {
            result.error(-1,"用户或密码错误!");
        }
        return result;

    }
}
